var institutionId;
var institutionName;
$.ajax({
    url: "/recruitmentManager/selectInstitutionOfLoginUser",
    type: "POST",
    async:false,
    success: function (result) {
        if (result.code == 0) {
            var idata = result.institutionOfLoginUser;
            console.log(idata);
            institutionId = idata['institutionId'];
            institutionName = idata['name']
            $("#getInstitutionName").val(institutionName);  //往input框里传值
            $('#institutionId1').val(institutionId);
            console.log("institutionId的值:"+$('#institutionId1').val());
            console.log("institutionName的值:"+$('#getInstitutionName').val());
        } else {
            layer.alert(result.msg);
        }
    }
});

$(function(){
    $('#selectAllPositionByInstitutionIdTable').bootstrapTable({
        url: "/recruitmentManager/selectAllPositionByInstitutionId",
        //data: JSON.stringify(institutionId),//参数
        method: 'get',
        queryParams: function (params) {
            return {
                institutionId: institutionId
            };

        },
        showRefresh: true,  //显示刷新按钮
        search: true,
        toolbar:'#toolbar',
        striped : true,     //设置为true会有隔行变色效果
        //sidePagination:'server',//服务器端分页默认接受rows和total
        pagination:true,//客户端分页
        sidePagination:'client',
        //idFiled:'selectPositionId',//必选属性
        pageNumber:1,
        pageSize:5,
        columns: [
            {
                field: 'checkId',
                checkbox:true,
                clickToSelect:true
            },
             {
                field: 'position_id',
                title: '职位编号'
            }
            , {
                field: 'name',
                title: '职称'
            },{
                field: 'ideal_mun',
                title: '建议人数'
            },{
                field: 'descrip',
                title: '职位描述'
            },{
                field: 'salary',
                title: '薪资'
            }]
    });
});

var vm = new Vue({
    el:'#uploadRecruitPlan',
    data:{
        showList:1,
        recruitId:'',
        title:'',
        //新增弹窗需要字段
        institutionId:institutionId,
        positionName:'',
        positionNum:'',
        recruitNum:'',
        startDate:'',
        stopDate:'',
        claim:'',
    },
    methods: {
        //***新增招聘计划的方法***
        //新增按钮
        add: function () {
            // 弹出新增对话框
            $('#selectPositionDialog').modal("show");
            //回显下拉框
            $("#positionName").empty();
            var optionName="请点击选择职位";
            $.ajax({
                type: "POST",
                url: "/recruitmentManager/selectPositionByInstitutionIdOfLoginUser",
                success: function (result) {
                    console.log(result);
                    var data = result.PositionByInstitutionIdOfLoginUser;
                    console.log("PositionByInstitutionIdOfLoginUser:"+data);
                    if (result.code == 0) {
                        $("#positionName").append("<option value=" + "prompt" + ">" + optionName + "</option>")

                        for (var i = 0; i < data.length; i++) {
                            $("#positionName").append("<option value=" + data[i]['positionId'] + ">" + data[i]['name'] + "</option>")
                        }

                    } else {
                        layer.alert(result.msg);
                    }
                }
            });
            var positionId;
            var institutionId2;

            //回显岗位人数
            //下拉框点击事件
            $("#positionName").change(function(){
                positionId = $("#positionName option:selected").val();
                institutionId2 = $('#institutionId1').val();
                //console.log("positionNum:"+vm.positionNum);
                if (positionId=="prompt"){//选中了提示的option
                    vm.positionNum=null
                    return
                }
/*                console.log("positionId:"+positionId);
                console.log("institutionId2:"+institutionId2);*/
                var da={"positionId":positionId,"institutionId":institutionId2};
                $.ajax({
                    type: "POST",//post方法默认是表单数据提交，用requestParameter接收，如果要用jason需要配置在conmmon.js中的第一行有配置
                    url: "/recruitmentManager/getPositionNumByInstitutionIdAndPositionId",
                    //data: JSON.stringify(institutionId,positionId),//参数
                    dataType : "JSON",
                    data :JSON.stringify(da),
                    success: function (result) {
                        // console.log("result"+result);

                        if (result.code == 0) {

                            vm.positionNum = result.PositionNumByInstitutionIdAndPositionId;

                        } else {
                            layer.alert(result.msg);
                        }
                    }
                });
            })},
        // 新增提交按钮
        submit: function () {
            //获取下拉列表所选的value
            var positionId = $("#positionName option:selected").val();
            if (positionId=="prompt") {//选中了提示的option
                layer.alert('请选择一个职位');
                vm.positionNum=null;
                return
            }
            if(vm.startDate<=vm.stopDate){//起始时间要求
                console.log("positionId:"+positionId);
                var data2={"institutionId":vm.institutionId,
                    "positionId":positionId,
                    "recruitNum":vm.recruitNum,
                    "startDate":vm.startDate,
                    "stopDate":vm.stopDate,
                    "claim":vm.claim
                };
                console.log("data2:"+data2);
                $.ajax({
                    type: "POST",//post方法默认是表单数据提交，用requestParameter接收，如果要用jason需要配置在conmmon.js中的第一行有配置
                    url: '/recruitmentManager/insertRecruit',//请求的controller
                    dataType : "JSON",
                    data :JSON.stringify(data2),

                    success: function (r) {
                        if (r.code === 0) {
                            layer.alert('添加成功');
                            //删除下拉框中当前选中的元素
                            $("#positionName option:selected").remove();
                            //更新表格
                            $('#selectAllPositionByInstitutionIdTable').bootstrapTable('refresh');
                        }
                    },
                    error: function () {
                        layer.alert('服务器没有返回数据，可能服务器忙，请重试');
                    }
                });
            }else {
                layer.alert('起始日期输入有误！');
                vm.startDate=null;
                vm.stopDate=null;
                return
            }

        },
        //列表查看招聘信息
        recruitList: function(){
                window.location.href = 'PublishRecruitment.html';
        },
        //删除
        del: function () {
            var rows = $("#selectAllPositionByInstitutionIdTable").bootstrapTable('getSelections');

            if (rows.length == 0) {
                layer.alert('请选择至少一条记录');
                return;
            } else {
                layer.confirm('您确定删除所选的招聘计划吗？', {
                    btn: ['确定', '取消'] //可以无限个按钮
                }, function(index, layero){
                    var ids = new Array();
                    //遍历所有选择的行数据，取每条数据对应的ID
                    $.each(rows, function(i, row) {
                        ids[i] = row['selectPositionId'];
                    });
                    console.log(ids);
                    $.ajax({
                        type: "POST",
                        url: "/student/stuDeleteCourse",
                        data: JSON.stringify(ids),
                        success : function(r) {
                            if(r.code === 0){
                                layer.alert('删除成功');
                                $('#selectAllPositionByInstitutionIdTable').bootstrapTable('refresh');
                            }else{
                                layer.alert(r.msg);
                            }
                        },
                        error : function() {
                            layer.alert('服务器没有返回数据，可能服务器忙，请重试');
                        }
                    });
                });
            }
        }
    }
})

